package com.cheyun.netsalev3.repository

import com.cheyun.netsalev3.bean.DaoDianDetailsParam
import com.cheyun.netsalev3.bean.drive.DriveArchiveData
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.bean.order.OrderKeliuData
import com.cheyun.netsalev3.utils.api.ApiCallback
import com.cheyun.netsalev3.utils.api.ApiClient
import com.cheyun.netsalev3.utils.api.ApiException
import com.cheyun.netsalev3.utils.api.BaseResp
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

class EditCustomStoreRepository() {
    fun updateData(data: DaoDianDetailsParam, onSuccess: () -> Unit) {
        ApiClient.getInstance().updateDaoDianDetails(data.id, data.uname,
                Integer.parseInt(data.arrpnum), data.arrivetime,
                data.rcvuid, data.phone, data.leavetime, data.brandgroup,data.adorderid.toString(),data.ordertype,data.isexit.toString())
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        onSuccess()
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun addData(data: DaoDianDetailsParam, onSuccess: () -> Unit) {
        ApiClient.getInstance().addDaoDianDetails(data.uname,
                Integer.parseInt(data.arrpnum), data.arrivetime,
                data.rcvuid, data.phone, data.leavetime, data.brandgroup,data.resid,data.reservetime)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        onSuccess()
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun IsPhone(phone: String, kFunction1: (item: String) -> Unit) {
        ApiClient.getInstance().checkPhone(phone)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        if (t.state == "y") {
                            kFunction1(t.msg!!)
                        }
                    }
                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun getScanResult(phone:String,rid:String,kFunction1: (item: ReserveInfo) -> Unit){
        ApiClient.getInstance().getReserveInfo("",phone,"",rid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ReserveInfo>>() {
                    override fun onSuccess(t: BaseResp<ReserveInfo>) {
                        if (t.state == "y") {
                            kFunction1(t.data!!)
                        }
                    }
                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun getArchiveByPhone(phone:String ,kFunction1: (item: DriveArchiveData) -> Unit){
        ApiClient.getInstance().getArchiveByPhone(phone)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DriveArchiveData>>() {
                    override fun onSuccess(t: BaseResp<DriveArchiveData>) {
                        if (t.state == "y") {
                            kFunction1(t.data!!)
                        }
                    }
                    override fun onError(ex: ApiException) {

                    }


                })
    }






}